/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generatorGraphics.interfaces;

import generatorGraphics.graphics.Graph;
import generatorGraphics.graphicsCategory.Category;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import org.jdesktop.swingx.JXCollapsiblePane;
import org.jdesktop.swingx.JXTaskPane;
import org.jdesktop.swingx.JXTaskPaneContainer;
import org.jfree.chart.ChartPanel;

/**
 *
 * @author Heitor
 */
public class JPGraphics  extends JPanel {
    
    private JFScreen screenMain;
    
    /*
     * Componentes Graficos
     */  
    public JPanel mainPanel;
    
    private JPanel jpWest; // Painel do Norte
    private JPanel jpCenter; // Painel Central
    private JPanel jpSouth; // Painel do Sul
    
    public JScrollPane jspGraph;
    private JPanel jpGraph;
    
    private JPopupMenu jpMenuGraph;
    
    public JScrollPane jspWestAux;
    private JPanel jpWestAux;
    
    private JTabbedPane jTabbedPane;
    private JXCollapsiblePane jCollapseOptions;
    private JXTaskPaneContainer jTaskPaneOptions;
    private JXTaskPane jTaskInformation;
    private JXTaskPane jTaskGraphics;
    private JXTaskPane jTaskFilter;
    private JXTaskPane jTaskZoom;
    
    private JComboBox jComboType;
    private JComboBox jComboUnit;
    private JComboBox jComboOptions;
    
    private JComboBox jComboFiltrate;
    private ArrayList<String> parametersFilter;
    
    private JPanel jpButtonsFeatures;
    private JButton jbUpdate;
    
    private JButton jbZoomHorizontal;
    private JButton jbZoomVertical;
    
    private JLabel jlGraph;
    
    private JLabel jlCategory;
    private JLabel jlCategoryName;
    
    private Point pointInitialZoom;
    private Point pointFinalZoom;
    
    private String unitSelected;
    
    private boolean stateHZoom = false;
    private boolean stateVZoom = false;
    
    private Category currentCategory;
    private Graph currentGraph;
    
    private JMenuItem jmFilter;
    private JMenuItem jmZoomGraph;
    private JMenuItem jmHelpGraph;
    
    /** Tipo de painel que será gerado.
     * - Tipo 1: 
     *      (jTaskGraphics: JCombo Type) + JPanel Graph
     * - Tipo 2: 
     *      (jTaskGraphics: JCombo Type + JCombo Unit) + JPanel Graph 
     * - Tipo 3:
     *      (jTaskGraphics: JCombo Type + JCombo Unit + JCombo Options) + JPanel Graph 
     */
     private int typeOptions;
    
     /** Varivel que indica quando o botao de atualizar foi clicado. */
     private boolean updateClicked = false;
    
     public int zoomX =0;
     public int zoomY =0;
     
    /** 
     * Método extra para geração de icones (Em analise)
     */
    private static final GridBagConstraints gbc;  
    static {  
        gbc = new GridBagConstraints();  
        gbc.gridx = 0;  
        gbc.gridy = 0;  
        gbc.weightx = 1.0;  
        gbc.weighty = 1.0;  
        gbc.fill = GridBagConstraints.BOTH;  
        gbc.anchor = GridBagConstraints.NORTHWEST;  
    }
    
    /**
     * Construtor
     */
    public JPGraphics(){
        typeOptions = 0;
        
        initComponents();
        
        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {  
            public void eventDispatched(AWTEvent event) {  
                String[] aux = event.paramString().split(",");
                if(aux.length>1){
                    if((event.getID() == 402)&&((aux[1].split("="))[1].equals("18"))){
                        if(screenMain.jToolBar.isVisible()){
                            screenMain.jToolBar.setVisible(false);
                            screenMain.jCollapsible.setVisible(false);
                        }else{
                            screenMain.jToolBar.setVisible(true);
                            screenMain.jCollapsible.setVisible(true);
                        }
                        screenMain.screenResize();
                    }
                }
            }  
        }, AWTEvent.KEY_EVENT_MASK);
        
        setBorder(null);
        setVisible(true);
    }
    
    public JPGraphics(JFScreen screen, Category cat){
        screenMain = screen;
        
        if(cat == null){
            currentCategory = null;
            typeOptions = 0;
        } else{
            currentCategory = cat;
            typeOptions = cat.getTypeOptions();
        }
        
        initComponents();
        
        setBorder(null);
        setVisible(true);
    }
    
    public JPGraphics(int width, int height){    
        typeOptions = 0;
        
        initComponents();
        
        setSize(width, height);
        setBorder(null);
        setVisible(true);
    }

    /**
     * Inicia componentes gráficos
     */
    private void initComponents(){
        
        setLookAndFeel();
        
        // Criando botão de atualizar o painel
        jpButtonsFeatures = new JPanel();
            jpButtonsFeatures.setLayout(new GridLayout(1, 3));
            JButton auxB1 = new JButton();
            auxB1.setVisible(false);
            JButton auxB2 = new JButton();
            auxB2.setVisible(false);
            
            jbUpdate = new JButton();
            jbUpdate.setText("Update");
            jbUpdate.setIcon(new ImageIcon(JPCategory.class.getResource("/generatorGraphics/images/update_object1.png")));
            jbUpdate.setContentAreaFilled(false);
            jbUpdate.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    updateActionPerformed(null);
                }
            });
            
            //update.setMaximumSize(new Dimension(20, 25));
            //jbUpdate.setPreferredSize(new Dimension(60, 25));
        
            //jpButtonsFeatures.add(auxB1);
            jpButtonsFeatures.add(jbUpdate);
            //jpButtonsFeatures.add(auxB2);
            
        // Iniciando o painel geral
        setLayout(new BorderLayout());
        
        jpWest = new JPanel();
            jpWestAux = new JPanel();
            jspWestAux = new JScrollPane();
            
            jTabbedPane = new JTabbedPane();
            jCollapseOptions = new JXCollapsiblePane(JXCollapsiblePane.Direction.LEFT);
            
            jCollapseOptions.setAnimated(false);
            jCollapseOptions.setCollapsed(false);
            
            BorderLayout layoutAux = new BorderLayout();
            jpWestAux.setLayout(layoutAux);
        
            jlGraph = new JLabel("Graphics", JLabel.CENTER);
                jlGraph.setFont(new Font("Courier", Font.BOLD, 14));
                jlGraph.setVisible(false);
                
            jComboType = new JComboBox();
            jComboUnit = new JComboBox();
            jComboOptions = new JComboBox();
            
            jTaskPaneOptions = new JXTaskPaneContainer();
            jTaskPaneOptions.setBackground(new Color(238, 238, 238));
            
            jTaskInformation = new JXTaskPane("Information");
            if(currentCategory != null){
                jlCategory = new JLabel("Category:");
                jlCategory.setFont(new Font("Courier", Font.PLAIN, 12));
                jlCategory.setVisible(true);
                jlCategoryName = new JLabel();
                jlCategory.setFont(new Font("Courier", Font.CENTER_BASELINE, 12));
                jlCategoryName.setText("  " + currentCategory.getName());
                jlCategoryName.setVisible(true);
                
                jTaskInformation.add(jlCategory);
                jTaskInformation.add(jlCategoryName);
            }
            
            jTaskGraphics = new JXTaskPane("Features Graphics");

            if(getCurrentCategory() != null){
                jlGraph.setVisible(true);
                
                ArrayList<Graph> graphList_aux = getCurrentCategory().getGraphicsList();
                
                // Colocados os objetos da categoria no JComboBox_Type
                for(int p=0; p<graphList_aux.size(); p++){
                    jComboType.addItem(graphList_aux.get(p));
                    jComboType.addItemListener(new ItemListener() {

                        @Override
                        public void itemStateChanged(ItemEvent e) {
                            Graph itemSelected = ((Graph) jComboType.getSelectedItem());
                            currentGraph = itemSelected;
                            updateJUnit();

                            screenMain.screenResize();
                        }
                    });
                }
                
                currentGraph = ((Graph) jComboType.getSelectedItem());
                
                // Colocando as unidades no JComboBox_Unit do primeiro objeto do JComboBox_Type
                jComboUnit.removeAll();
                jComboUnit = new JComboBox();
                if (jComboType.getSelectedObjects().length != 0){
                    for(int u=0; u<((Graph) jComboType.getItemAt(0)).getUnitList().size(); u++){
                        jComboUnit.addItem(((Graph) jComboType.getItemAt(0)).getUnitList().get(u));
                    }
                }
                
                // Colocando as opções no JComboBox_Options do primeiro objeto do JComboBox_Type
                if (jComboType.getSelectedObjects().length != 0){
                    if (jComboUnit.getSelectedObjects().length != 0){
                        //int unitAux = ((Graph) jComboType.getItemAt(0)).getIdUnit((String) jComboUnit.getItemAt(0));  
                        String unitAux = ((String) jComboUnit.getItemAt(0));
                        
                        for(int u=0; u<((Graph) jComboType.getItemAt(0)).getOptionsList(unitAux).size(); u++){
                            jComboOptions.addItem(((Graph) jComboType.getItemAt(0)).getOptionsList(unitAux).get(u));
                        }
                    }
                }

                if (this.typeOptions == 0){

                } else if (this.typeOptions == 1){
                    if(jComboType.getSelectedObjects().length != 0){
                        jTaskGraphics.add(new JLabel("Type: "));
                        jTaskGraphics.add(jComboType);
                        jpButtonsFeatures.setBackground(jTaskGraphics.getBackground());
                        jTaskGraphics.add(jpButtonsFeatures);
                    }
                } else if(this.typeOptions == 2){
                    
                    if(jComboType.getSelectedObjects().length != 0){
                        jTaskGraphics.add(new JLabel("Type: "));
                        jTaskGraphics.add(jComboType);
                        if(jComboUnit.getSelectedObjects().length != 0){
                            jTaskGraphics.add(new JLabel("Unit: "));
                            jTaskGraphics.add(jComboUnit);
                        }
                        jpButtonsFeatures.setBackground(jTaskGraphics.getBackground());
                        jTaskGraphics.add(jpButtonsFeatures);
                    }
                } else if (this.typeOptions == 3) {
                    if(jComboType.getSelectedObjects().length != 0){
                        jTaskGraphics.add(new JLabel("Type: "));
                        jTaskGraphics.add(jComboType);
                        if(jComboUnit.getSelectedObjects().length != 0){
                            jTaskGraphics.add(new JLabel("Unit: "));
                            jTaskGraphics.add(jComboUnit);
                        }
                        if(jComboOptions.getSelectedObjects().length != 0){
                            jTaskGraphics.add(new JLabel("Options: "));
                            jTaskGraphics.add(jComboOptions);
                        }
                        jpButtonsFeatures.setBackground(jTaskGraphics.getBackground());
                        jTaskGraphics.add(jpButtonsFeatures);
                    }
                } else if (this.typeOptions == 4) {
                    if(jComboType.getSelectedObjects().length != 0){
                        jTaskGraphics.add(new JLabel("Type: "));
                        jTaskGraphics.add(jComboType);
                        if(jComboUnit.getSelectedObjects().length != 0){
                            jTaskGraphics.add(new JLabel("Unit: "));
                            jTaskGraphics.add(jComboUnit);
                        }
                        jpButtonsFeatures.setBackground(jTaskGraphics.getBackground());
                        jTaskGraphics.add(jpButtonsFeatures);
                        JButton add = new JButton("Add");
                        add.addActionListener(new ActionListener() {
                            
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                System.out.println("Button Add Clicked");

                            }
                        });
                        jTaskGraphics.add(add);
                    }
                }
                
                if (jTaskGraphics.getComponents().length == 0){
                    jTaskGraphics.setVisible(false);
                }
                
                jTaskZoom = new JXTaskPane("Zoom Graph");
                
                    jbZoomHorizontal = new JButton("Horizontal");
                    jbZoomHorizontal.addMouseListener(new MouseAdapter() {
                        
                        @Override
                        public void mouseClicked(MouseEvent e){
                            stateHZoom = true;
                            stateVZoom = false;
                            jpGraph.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
                        }
                    });
                    jbZoomHorizontal.setVisible(true);
                    
                    jbZoomVertical = new JButton("Vertical");
                    jbZoomVertical.addMouseListener(new MouseAdapter() {
                        
                        @Override
                        public void mouseClicked(MouseEvent e){
                            stateVZoom = true;
                            stateHZoom = false;
                            jpGraph.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
                        }
                    });
                    jbZoomVertical.setVisible(true);
                    
                    jTaskZoom.add(jbZoomHorizontal);
                    jTaskZoom.add(jbZoomVertical);
                    jTaskZoom.setVisible(false);
                    
                jTaskPaneOptions.add(jTaskInformation);
                jTaskPaneOptions.add(jTaskGraphics);
                jTaskPaneOptions.add(jTaskZoom);
                
                /* Atualiza o painel de opções */
                jpWestAux.add(BorderLayout.NORTH, jlGraph);
                jpWestAux.add(BorderLayout.CENTER, jTaskPaneOptions);

                jCollapseOptions.removeAll();
                jCollapseOptions.add(jpWestAux);
                jCollapseOptions.setBackground(new Color(238, 238, 238));
                
                jspWestAux = new JScrollPane(jCollapseOptions);
                jspWestAux.setBorder(null);
                
                jTabbedPane.setTabPlacement(JTabbedPane.RIGHT);
                jTabbedPane.addTab("", jspWestAux);
                jTabbedPane.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent me) {
                        
                        if(jTabbedPane.hasFocus()){
                        if(jCollapseOptions.isCollapsed()){
                            jCollapseOptions.setCollapsed(false);
                        } else{
                            jCollapseOptions.setCollapsed(true);
                        }

                        screenMain.screenResize();
                        }
                    }
                });
                jTabbedPane.setBackground(new Color(238, 238, 238));
                jTabbedPane.setBorder(null);
                
            }
            
        jpCenter = new JPanel();
            jpGraph = new JPanel();
            jspGraph = new JScrollPane(jpGraph);
            jspGraph.setBorder(null);
            jpCenter.add(jspGraph);
            
        jpSouth = new JPanel();
        
        removeAll();
        add(BorderLayout.WEST, jTabbedPane);
        add(BorderLayout.CENTER, jpCenter);
        add(BorderLayout.SOUTH, jpSouth);
        
        setBorder(null);
        setVisible(true);
    }

    /**
     * @return the jpGraph
     */
    public JPanel getJpGraph() {
        return jpGraph;
    }

    /**
     * @param jpGraph the jpGraph to set
     */
    public void setJpGraph(JPanel jpGraph) {
        this.jpGraph = jpGraph;
        
        componentGraphResize(null);
    }

    /**
     * @return the jComboType
     */
    public JComboBox getjComboType() {
        return jComboType;
    }

    /**
     * @param jComboType the jComboType to set
     */
    public void setjComboType(JComboBox jComboType) {
        this.jComboType = jComboType;
    }
    
    /**
     * @return the jComboUnit
     */
    public JComboBox getjComboUnit() {
        return jComboUnit;
    }

    /**
     * @param jComboUnit the jComboUnit to set
     */
    public void setjComboUnit(JComboBox jComboUnit) {
        this.jComboUnit = jComboUnit;
    }
    
    /**
     * @return the jComboOptions
     */
    public JComboBox getjComboOptions() {
        return jComboOptions;
    }

    /**
     * @param jComboOptions the jComboOptions to set
     */
    public void setjComboOptions(JComboBox jComboOptions) {
        this.jComboOptions = jComboOptions;
    }
    
    /**
     * Método do botão de atualizar o gráfico:
     * - Requisitos:
     * -- Seleção de um tipo de gráfico
     * -- Seleção de um tipo de unidade (Opcional)
     * -- Seleção de um tipo de opção (Opcional)
     */
    private void updateActionPerformed(JPanel graph) {
        // Botão já foi clicado pelo menos uma vez
        updateClicked = true;
        
        zoomX =0;
        zoomY =0;
        
        try{
        jpGraph = new JPanel();
        if(graph == null){
            if (currentGraph != null){
                if (jComboUnit.getSelectedObjects().length != 0){
                    unitSelected = jComboUnit.getSelectedItem().toString();
                    jpGraph = currentGraph.getChartPanel(jComboUnit.getSelectedItem().toString());
                } else {
                    jpGraph = currentGraph.getChartPanel();
                }            
                jTaskZoom.setVisible(true);
            } else{
                jTaskZoom.setVisible(false);
            }   

            jpGraph.addMouseListener(new MouseAdapter() {            
                @Override
                public void mouseClicked(MouseEvent e){            
                    if (e.getClickCount() > 1 
                            || (e.getButton() == MouseEvent.BUTTON2)
                            || (e.getButton() == MouseEvent.BUTTON3)) {
                        stateHZoom = false;
                        stateVZoom = false;                    
                        jpGraph.setCursor(null);
                    }
                }

                @Override
                public void mousePressed(MouseEvent e){
                    if (stateHZoom){
                        pointInitialZoom = e.getLocationOnScreen();
                    } else if (stateVZoom){                
                        pointInitialZoom = e.getLocationOnScreen();
                    }
                }

                @Override
                public void mouseReleased(MouseEvent e){
                    int auxZoom = 0;
                    if (stateHZoom){                    
                        pointFinalZoom = e.getLocationOnScreen();                    
                        auxZoom = pointFinalZoom.x - pointInitialZoom.x;
                        if(zoomX == 0){
                            if(auxZoom > 0){
                                zoomX = zoomX + auxZoom;
                            }
                        } else{
                            if (auxZoom < 0){
                                if(zoomX < (auxZoom * (-1))){
                                    zoomX =0;
                                } else{
                                    zoomX = zoomX + auxZoom;
                                }
                            } else{
                                zoomX = zoomX + auxZoom;
                            }
                        }                    
                    } else if (stateVZoom){                
                        pointFinalZoom = e.getLocationOnScreen();                    
                        auxZoom = pointFinalZoom.y - pointInitialZoom.y;
                        if(zoomY == 0){
                            if(auxZoom > 0){
                                zoomY = zoomY + auxZoom;
                            }
                        } else{
                            if (auxZoom < 0){
                                if(zoomY < (auxZoom * (-1))){
                                    zoomY =0;
                                } else{
                                    zoomY = zoomY + auxZoom;
                                }
                            } else{
                                zoomY = zoomY + auxZoom;
                            }
                        }
                    }                
                    screenMain.screenResize();
                }
            });

            /** Mouse botão do meio, de rolagem */
            jpGraph.addMouseWheelListener(new MouseWheelListener() {
                @Override
                public void mouseWheelMoved(MouseWheelEvent e) {
                    stateHZoom = false;
                    stateVZoom = false;                
                    jpGraph.setCursor(null);
                }
            });

            //PopupMenu ChartPanel
            jpMenuGraph = ((ChartPanel) jpGraph).getPopupMenu();
            for(int y=0; y<jpMenuGraph.getComponents().length; y++){
                if((jpMenuGraph.getComponents()[y]).getClass().getName().equals("javax.swing.JMenu")){
                    if(((JMenu) jpMenuGraph.getComponents()[y]).getText().equals("Ampliar")
                            || ((JMenu) jpMenuGraph.getComponents()[y]).getText().equals("Reduzir")
                            || ((JMenu) jpMenuGraph.getComponents()[y]).getText().equals("Escala automática")){
                        //(jpMenuGraph.getComponents()[y]).setVisible(false);
                    }                    
                    if((jpMenuGraph.getComponents()[y-1]).getClass().getName().equals("javax.swing.JPopupMenu$Separator")){
                        (jpMenuGraph.getComponents()[y-1]).setVisible(false);
                    }
                }
            }

            if(currentGraph.getIsFilter()){
                jpMenuGraph.add(new JPopupMenu.Separator());
                jmFilter = new JMenuItem("Filtrate");
                jmFilter.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(currentGraph.getIsFilter()){
                            Object x = currentGraph.getFilter(unitSelected.toString());

                            final JFFilter jfFilter = new JFFilter(x);
                            jfFilter.jButton1.addActionListener(new ActionListener() {

                                @Override
                                public void actionPerformed(ActionEvent e) {

                                    updateActionPerformed(currentGraph.filtrate(unitSelected.toString(), jfFilter.parameter));
                                    jfFilter.setVisible(false);
                                }
                            });
                            jfFilter.setVisible(true);
                        }

                    }
                });
                jpMenuGraph.add(jmFilter);
            }

            jpMenuGraph.add(new JPopupMenu.Separator());
            jmZoomGraph = new JMenuItem("Zoom Default");
            jmZoomGraph.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    zoomX = 0;
                    zoomY = 0;
                    jpGraph.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

                    screenMain.screenResize();
                }
            });
            jpMenuGraph.add(jmZoomGraph);

            jpMenuGraph.add(new JPopupMenu.Separator());
            jmHelpGraph = new JMenuItem("Help");
            jmHelpGraph.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, currentGraph.getHelp(), "Help", 1);
                }
            });
            jpMenuGraph.add(jmHelpGraph);
        } else{
            jpGraph = graph;
        }
        } catch (Exception e){
            System.out.println("[Erro-JPGraphics-updateActionPerformed]" + e.toString());
            jpGraph = new JPanel();
        }
        jpGraph.setVisible(true);
        
        screenMain.screenResize();
        
        if(currentGraph.getIsFilter()){
            //updateJFilter();
        }
    }
    
    /**
     * Redimensiona o painel do gráfico
     * @param dim 
     */
    public void componentGraphResize(Dimension dim){
        
        if((dim != null) && (currentGraph != null)){
            int xAux =0; // X JPGraph
            int xSAux =0; // X JSPGraph
            int yAux =0; // Y JPGraph
            int ySAux =0; // Y JSPGraph
            
            if(jCollapseOptions.isCollapsed()){
                xAux = ((int) dim.getWidth()-35+ zoomX);
                xSAux = ((int) dim.getWidth()-35);
                if (screenMain.jCollapsible.isCollapsed()){
                    yAux = ((int) dim.getHeight())-25+zoomY;
                    ySAux = ((int) dim.getHeight())-8;
                } else{
                    yAux = ((int) dim.getHeight())-screenMain.jCollapsible.getHeight()-20 +zoomY;
                    ySAux = ((int) dim.getHeight())-screenMain.jCollapsible.getHeight()+30;
                }
                
                this.jpGraph.setPreferredSize(new Dimension(xAux, yAux));
                this.jspGraph.setPreferredSize(new Dimension(xSAux, ySAux));
            } else{
                xAux = ((int) dim.getWidth()-jTabbedPane.getWidth()-30 +zoomX);
                xSAux = ((int) dim.getWidth()-jTabbedPane.getWidth()-15);
                if (screenMain.jCollapsible.isCollapsed()){
                    yAux = ((int) dim.getHeight())-25 + zoomY;
                    ySAux = ((int) dim.getHeight())-8;
                } else{
                    yAux = ((int) dim.getHeight())-screenMain.jCollapsible.getHeight()-20 + zoomY;
                    ySAux = ((int) dim.getHeight())-screenMain.jCollapsible.getHeight()+30;
                }

                this.jpGraph.setPreferredSize(new Dimension(xAux, yAux));
                this.jspGraph.setPreferredSize(new Dimension(xSAux, ySAux));
            }

            this.jspGraph.setViewportView(this.jpGraph);
            
            if(zoomX > 0){
                this.jspGraph.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                if(zoomY > 0){
                    this.jspGraph.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
                } else{
                    this.jspGraph.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
                }
            } else{
                this.jspGraph.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                if(zoomY > 0){
                    this.jspGraph.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
                } else{
                    this.jspGraph.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
                }
            }

            if (updateClicked){
                this.jspGraph.setVisible(true);
            } else{
                this.jspGraph.setVisible(false);
            }
            
        }
        
    }
    
    public void updateJUnit(){
        
        if(currentGraph != null){
            
            jComboUnit.removeAll();
            jComboUnit = new JComboBox();

            jComboOptions.removeAll();
            jComboOptions = new JComboBox();

            for(int u=0; u<currentGraph.getUnitList().size(); u++){
                jComboUnit.addItem(currentGraph.getUnitList().get(u));
                jComboUnit.addItemListener(new ItemListener() {

                    @Override
                    public void itemStateChanged(ItemEvent e) {

                        unitSelected = ((String) jComboUnit.getSelectedItem());
                        updateJOptions();
                        screenMain.screenResize();
                    }
                });
            }

            for(int y=0; y<currentGraph.getOptionsList((String) jComboUnit.getItemAt(0)).size(); y++){
                jComboOptions.addItem(currentGraph.getOptionsList((String) jComboUnit.getItemAt(0)).get(y));
            }
            
        } else {
            jComboUnit = new JComboBox();
            jComboOptions = new JComboBox();
        }

        jTaskPaneOptions.removeAll();
        jTaskPaneOptions = new JXTaskPaneContainer();
        jTaskPaneOptions.setBackground(new Color(238, 238, 238));
        
        jTaskInformation = new JXTaskPane("Information");
        buildJTaskInformation();
        
        jTaskGraphics = new JXTaskPane("Features Graphics");
        buildJTaskGraphics();
        
        jTaskZoom = new JXTaskPane("Zoom Graph");
        buildJTaskZoom();
  
        jTaskPaneOptions.add(jTaskInformation);
        jTaskPaneOptions.add(jTaskGraphics);
        jTaskPaneOptions.add(jTaskZoom);
        
        BorderLayout layoutAux = new BorderLayout();
        jpWestAux.removeAll();
        jpWestAux.setLayout(layoutAux);
        jpWestAux.add(BorderLayout.NORTH, jlGraph);
        jpWestAux.add(BorderLayout.CENTER, jTaskPaneOptions);
        jpWestAux.repaint();
        
        jCollapseOptions.removeAll();
        jCollapseOptions.add(jpWestAux);
        
        jspWestAux.setViewportView(jCollapseOptions);
        jspWestAux.setBorder(null);
        jspWestAux.setBackground(new Color(238, 238, 238));

        jTabbedPane.setTabPlacement(JTabbedPane.RIGHT);
        jTabbedPane.addTab("", jspWestAux);
        jTabbedPane.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                
                if(jTabbedPane.hasFocus()){
                if(jCollapseOptions.isCollapsed()){
                    jCollapseOptions.setCollapsed(false);
                } else{
                    jCollapseOptions.setCollapsed(true);
                }
                
                screenMain.screenResize();
                }
                
            }
        });
        jTabbedPane.setBackground(new Color(238, 238, 238));
        jTabbedPane.setVisible(true);
        jTabbedPane.setBorder(null);
        
        removeAll();
        add(BorderLayout.WEST, jTabbedPane);
        add(BorderLayout.CENTER, jpCenter);
        add(BorderLayout.SOUTH, jpSouth);
    }
    
    public void updateJOptions(){
        if(currentGraph != null){
            if(unitSelected == null){
                jComboUnit.removeAll();
                jComboUnit = new JComboBox();
                
                jComboOptions.removeAll();
                jComboOptions = new JComboBox();

                for(int u=0; u<currentGraph.getUnitList().size(); u++){
                    jComboUnit.addItem(currentGraph.getUnitList().get(u));
                    jComboUnit.addItemListener(new ItemListener() {

                        @Override
                        public void itemStateChanged(ItemEvent e) {

                            unitSelected = ((String) jComboUnit.getSelectedItem());
                            updateJOptions();
                            screenMain.screenResize();
                        }
                    });
                }

                for(int y=0; y<currentGraph.getOptionsList((String) jComboUnit.getItemAt(0)).size(); y++){
                    jComboOptions.addItem(currentGraph.getOptionsList((String) jComboUnit.getItemAt(0)).get(y));
                }
            } else {
                jComboOptions.removeAll();
                jComboOptions = new JComboBox();
                for(int y=0; y<currentGraph.getOptionsList(unitSelected).size(); y++){
                    jComboOptions.addItem(currentGraph.getOptionsList(unitSelected).get(y));
                }
            }
        } else {
            jComboUnit = new JComboBox();
            jComboOptions = new JComboBox();
        }

        jTaskPaneOptions.removeAll();
        jTaskPaneOptions = new JXTaskPaneContainer();
        jTaskPaneOptions.setBackground(new Color(238, 238, 238));
        
        jTaskInformation = new JXTaskPane("Information");
        buildJTaskInformation();
        
        jTaskGraphics = new JXTaskPane("Features Graphics");
        buildJTaskGraphics();
        
        jTaskZoom = new JXTaskPane("Zoom Graph");
        buildJTaskZoom();
  
        jTaskPaneOptions.add(jTaskInformation);
        jTaskPaneOptions.add(jTaskGraphics);
        jTaskPaneOptions.add(jTaskZoom);
        
        BorderLayout layoutAux = new BorderLayout();
        jpWestAux.removeAll();
        jpWestAux.setLayout(layoutAux);
        jpWestAux.add(BorderLayout.NORTH, jlGraph);
        jpWestAux.add(BorderLayout.CENTER, jTaskPaneOptions);
        jpWestAux.repaint();
        
        jCollapseOptions.removeAll();
        jCollapseOptions.add(jpWestAux);
        
        jspWestAux.setViewportView(jCollapseOptions);
        jspWestAux.setBorder(null);
        jspWestAux.setBackground(new Color(238, 238, 238));

        jTabbedPane.setTabPlacement(JTabbedPane.RIGHT);
        jTabbedPane.addTab("", jspWestAux);
        jTabbedPane.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                
                if(jTabbedPane.hasFocus()){
                if(jCollapseOptions.isCollapsed()){
                    jCollapseOptions.setCollapsed(false);
                } else{
                    jCollapseOptions.setCollapsed(true);
                }
                
                screenMain.screenResize();
                }
                
            }
        });
        jTabbedPane.setBackground(new Color(238, 238, 238));
        jTabbedPane.setVisible(true);
        jTabbedPane.setBorder(null);
        
        removeAll();
        add(BorderLayout.WEST, jTabbedPane);
        add(BorderLayout.CENTER, jpCenter);
        add(BorderLayout.SOUTH, jpSouth);
    }
    
    public void updateJFilter(){

        jTaskPaneOptions.removeAll();
        jTaskPaneOptions = new JXTaskPaneContainer();
        jTaskPaneOptions.setBackground(new Color(238, 238, 238));
        
        jTaskFilter = new JXTaskPane("Filter");
        buildJTaskFilter();
  
        jTaskPaneOptions.add(jTaskInformation);
        jTaskPaneOptions.add(jTaskGraphics);
        jTaskPaneOptions.add(jTaskFilter);
        jTaskPaneOptions.add(jTaskZoom);
        
        BorderLayout layoutAux = new BorderLayout();
        jpWestAux.removeAll();
        jpWestAux.setLayout(layoutAux);
        jpWestAux.add(BorderLayout.NORTH, jlGraph);
        jpWestAux.add(BorderLayout.CENTER, jTaskPaneOptions);
        jpWestAux.repaint();
        
        jCollapseOptions.removeAll();
        jCollapseOptions.add(jpWestAux);
        
        jspWestAux.setViewportView(jCollapseOptions);
        jspWestAux.setBorder(null);
        jspWestAux.setBackground(new Color(238, 238, 238));

        jTabbedPane.setTabPlacement(JTabbedPane.RIGHT);
        jTabbedPane.addTab("", jspWestAux);
        jTabbedPane.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                
                if(jTabbedPane.hasFocus()){
                if(jCollapseOptions.isCollapsed()){
                    jCollapseOptions.setCollapsed(false);
                } else{
                    jCollapseOptions.setCollapsed(true);
                }
                
                screenMain.screenResize();
                }
                
            }
        });
        jTabbedPane.setBackground(new Color(238, 238, 238));
        jTabbedPane.setVisible(true);
        jTabbedPane.setBorder(null);
        
        removeAll();
        add(BorderLayout.WEST, jTabbedPane);
        add(BorderLayout.CENTER, jpCenter);
        add(BorderLayout.SOUTH, jpSouth);
    }
    
    /**
     * @return the currentCategory
     */
    public Category getCurrentCategory() {
        return currentCategory;
    }
    
    private static void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
        }
    }

    private void buildJTaskGraphics() {
        
        if (this.typeOptions == 0){

        } else if (this.typeOptions == 1){
            jTaskGraphics.add(new JLabel("Type: "));
            jTaskGraphics.add(jComboType);
            jTaskGraphics.add(jpButtonsFeatures);
        } else if(this.typeOptions == 2){
            jTaskGraphics.add(new JLabel("Type: "));
            jTaskGraphics.add(jComboType);
            if(jComboUnit.getSelectedObjects().length != 0){
                jTaskGraphics.add(new JLabel("Unit: "));
                jTaskGraphics.add(jComboUnit);
            }
            jTaskGraphics.add(jpButtonsFeatures);
        } else if (this.typeOptions == 3) {
            if(jComboType.getSelectedObjects().length != 0){
                jTaskGraphics.add(new JLabel("Type: "));
                jTaskGraphics.add(jComboType);
                if(jComboUnit.getSelectedObjects().length != 0){
                    jTaskGraphics.add(new JLabel("Unit: "));
                    jTaskGraphics.add(jComboUnit);
                }
                if(jComboOptions.getSelectedObjects().length != 0){
                    jTaskGraphics.add(new JLabel("Options: "));
                    jTaskGraphics.add(jComboOptions);
                }
                jTaskGraphics.add(jpButtonsFeatures);
            }
        } else if (this.typeOptions == 4) {
            jTaskGraphics.add(new JLabel("Type: "));
            jTaskGraphics.add(jComboType);
            if(jComboUnit.getSelectedObjects().length != 0){
                jTaskGraphics.add(new JLabel("Unit: "));
                jTaskGraphics.add(jComboUnit);
            }
            jTaskGraphics.add(jpButtonsFeatures);
            
            JButton add = new JButton("Add");
            add.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Button Add Clicked");

                }
            });
            jTaskGraphics.add(add);
        }

        if (jTaskGraphics.getComponents().length == 0){
            jTaskGraphics.setVisible(false);
        }
    }

    private void buildJTaskZoom() {
        jbZoomHorizontal = new JButton("Horizontal");
        jbZoomHorizontal.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e){
                if(!stateHZoom){
                    stateHZoom = true;
                    stateVZoom = false;
                    jpGraph.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
                } else{
                    stateHZoom = false;
                    stateVZoom = false;
                    jpGraph.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                }
            }
        });
        jbZoomHorizontal.setVisible(true);

        jbZoomVertical = new JButton("Vertical");
        jbZoomVertical.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e){
                if(!stateVZoom){
                    stateVZoom = true;
                    stateHZoom = false;
                    jpGraph.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
                } else{
                    stateVZoom = false;
                    stateHZoom = false;
                    jpGraph.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                }
            }
        });
        jbZoomVertical.setVisible(true);

        jTaskZoom.add(jbZoomHorizontal);
        jTaskZoom.add(jbZoomVertical);
        jTaskZoom.setVisible(false);
    }

    private void buildJTaskInformation() {
        if(currentCategory != null){
            jlCategory = new JLabel("Category:");
            jlCategory.setFont(new Font("Courier", Font.PLAIN, 12));
            jlCategory.setVisible(true);
            jlCategoryName = new JLabel();
            jlCategory.setFont(new Font("Courier", Font.CENTER_BASELINE, 12));
            jlCategoryName.setText("  " + currentCategory.getName());
            jlCategoryName.setVisible(true);

            jTaskInformation.add(jlCategory);
            jTaskInformation.add(jlCategoryName);
        }
    }
    
    private void buildJTaskFilter(){
        if(currentGraph != null){
            Label itemsLabel = new Label("Items:");
            itemsLabel.setForeground(Color.black);
            jTaskFilter.add(itemsLabel);
            Object parameters = currentGraph.getFilter(unitSelected.toString());
            jComboFiltrate = new JComboBox();
            for(int i=0; i< ((ArrayList<String>)parameters).size(); i++){
                jComboFiltrate.addItem(((ArrayList<String>)parameters).get(i));
            }
            jTaskFilter.add(jComboFiltrate);
            JButton buttonAdd = new JButton("Add");
            parametersFilter = new ArrayList<>();
            buttonAdd.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    parametersFilter.add(jComboFiltrate.getSelectedItem().toString());
                    jComboFiltrate.removeItem(jComboFiltrate.getSelectedItem());
                }
            });
            jTaskFilter.add(buttonAdd);
            JButton buttonDone = new JButton("Done");
            buttonDone.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {

                    updateActionPerformed(currentGraph.filtrate(unitSelected.toString(), parametersFilter));
                }
            });
            jTaskFilter.add(buttonDone);
            
        }
    }
    
}
